package com.zzedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.domain.ZzUploadFile;
import com.zzedu.enmuns.FileRelationTypeEnum;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.service.IZzUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 上传APK
 */
@RestController
@RequestMapping("/apk")
public class ApkController {

    @Autowired
    private IOssClientService ossClientService;


    @Autowired
    private IZzUploadFileService zzUploadFileService;

    /**
     * 获取下载连接
     * apk/getDownUrl/1
     * @param type 1-大屏 2-安卓
     * @return
     */
    @RequestMapping("/getDownUrl/{type}")
    public AjaxResult getDownUrl(@PathVariable("type") int type) {
        LambdaQueryWrapper<ZzUploadFile> zzUploadFileLambdaQueryWrapper = Wrappers.lambdaQuery(ZzUploadFile.class);
        FileRelationTypeEnum fileRelationTypeEnum = FileRelationTypeEnum.APK_AND;
        if (1 == type) {
            fileRelationTypeEnum = FileRelationTypeEnum.APK_BG;
        }
        zzUploadFileLambdaQueryWrapper.eq(ZzUploadFile::getRelationType, fileRelationTypeEnum.getCode());
        zzUploadFileLambdaQueryWrapper.orderByDesc(ZzUploadFile::getId).orderByDesc(ZzUploadFile::getGmtCreate).last("limit 1");
        ZzUploadFile zzUploadFile = zzUploadFileService.getOne(zzUploadFileLambdaQueryWrapper);
        return AjaxResult.success(ossClientService.convertURL(zzUploadFile.getUrl()));
    }
}
